#include "console.h"
#include "ui_console.h"
#include "QDate"
#include "QDateTime"

console::console(QWidget *parent, Script_thread *_script) :
	QWidget(parent),
	ui(new Ui::console),
	script(_script)
{
	ui->setupUi(this);    
	QList<QSerialPortInfo> infolist;
	QList<QSerialPortInfo>::iterator iter;
	QSerialPortInfo     info;

	infolist = info.availablePorts();

	read_data_running = 0;

	start_flag = false;

}

console::~console()
{
	delete ui;
}

void console::set_consolecolor(int d)
{
	switch(d){
		case 0:
			ui->textEdit->setTextColor(Qt::black);
			ui->textEdit->setTextBackgroundColor(Qt::white);
			break;
		case 30:
			ui->textEdit->setTextColor(Qt::black);
			break;
		case 31:
			ui->textEdit->setTextColor(Qt::red);
			break;
		case 32:
			ui->textEdit->setTextColor(Qt::green);
			break;
		case 33:
			ui->textEdit->setTextColor(Qt::yellow);
			break;
		case 34:
			ui->textEdit->setTextColor(Qt::blue);
			break;
		case 35:
			ui->textEdit->setStyleSheet(QLatin1String("color: rgb(255, 0, 255)"));
			break;
		case 36:
			ui->textEdit->setTextColor(Qt::cyan);
			break;
		case 37:
			ui->textEdit->setTextColor(Qt::white);
			break;
		case 40:
			ui->textEdit->setTextBackgroundColor(Qt::black);
			break;
		case 41:
			ui->textEdit->setTextBackgroundColor(Qt::red);
			break;
		case 42:
			ui->textEdit->setTextBackgroundColor(Qt::green);
			break;
		case 43:
			ui->textEdit->setTextBackgroundColor(Qt::yellow);
			break;
		case 44:
			ui->textEdit->setTextBackgroundColor(Qt::blue);
			break;
		case 45:
			ui->textEdit->setStyleSheet(QLatin1String("background-color: rgb(255, 0, 255)"));
			break;
		case 46:
			//ui->textEdit->setStyleSheet(QLatin1String("background-color: cyan"));
			ui->textEdit->setTextBackgroundColor(Qt::cyan);
			break;
		case 47:
			//ui->textEdit->setStyleSheet(QLatin1String("background-color: white"));
			ui->textEdit->setTextBackgroundColor(Qt::white);
			break;
	}
}

static QString get_time(void)
{
	QDateTime time = QDateTime::currentDateTime();
	QString st = time.toString("[yyyy-MM-dd hh:mm:ss.zzz] ");
	return st;
}

void *console::add_check_rule(QString rule)
{
	QRegExp *rx = new QRegExp(rule);
	checklist.append(rx);

	return rx;
}

void console::delete_rule(void *rule)
{
	checklist.removeOne((QRegExp*)rule);
}

void console::check_line(QString line)
{
	int i;
	for(i = 0; i < checklist.size(); i++){
		int pos = checklist[i]->indexIn(line);
		if(pos != -1){
			QString result = checklist[i]->cap();
		}
	}
}

#if 0
void console::Read_Data()
{
	if(read_data_running == true){
		return;
	}
	read_data_running = true;
	QByteArray  buf;

	while(1){
		buf = port->readAll();
		if(buf.length() == 0){
			break;
		}

		if(buf.isEmpty()){
			goto exit;
		}

		QString str(buf);
		QByteArray line;


		QFile file(log_filename);
		if(!file.exists()){
			if(!file.open(QIODevice::WriteOnly | QIODevice::Text)){
				qDebug() << file.errorString();
			}
		}else{
			if(!file.open(QIODevice::Append | QIODevice::Text)){
				qDebug() << file.errorString();
			}
		}

		QDataStream sfile(&file);
		sfile << buf;
		file.close();

		receive_data += buf;

		while(1){
			int end = receive_data.indexOf("\n");
			if(end != -1){
				QString ctime = get_time();
				QString sline;
				ui->textEdit->insertPlainText(ctime);
				sline.append(ctime);
				line = receive_data.mid(0,end);

				if(line[line.length()-2] == '\r'){
					line.remove(line.length()-2,1);
				}
				receive_data = receive_data.mid(end+1);

				while(1){
					int st = line.indexOf("\033[");
					if(st != -1){
						QByteArray s1 = line.mid(0,st);
						sline.append(s1);

						ui->textEdit->insertPlainText(s1);
						int e = line.indexOf("m",st);
						QByteArray colorp = line.mid(st+2,e-2);

						int e2 = colorp.indexOf(";");
						if(e2 != -1){
							QByteArray colorp1 = colorp.mid(0,e2);
							set_consolecolor(colorp1.toInt());

							colorp = colorp.mid(e2+1);
							set_consolecolor(colorp.toInt());
						}else{
							set_consolecolor(colorp.toInt());
						}

						line = line.mid(e+1);

					}else{
						sline.append(line);
						ui->textEdit->insertPlainText(line);
						ui->textEdit->moveCursor(QTextCursor::End);
						break;
					}
				}
				check_line(sline);
			}else{
				break;
			}
		}
	}

exit:
	read_data_running = false;
}
#endif

void console::printToText(QString str)
{

	ui->textEdit->insertPlainText(str);
	ui->textEdit->moveCursor(QTextCursor::End);
}

void console::start_test(bool flag, QString portname,int baudrate)
{
	if(flag == start_flag){
		return;
	}

	if(!flag){
		if(td){
			delete com;

			td->quit();
			td->wait();
			delete td;
			td = NULL;
		}

		start_flag = false;
	}else{

		start_flag = true;

		QDateTime time = QDateTime::currentDateTime();
		QString st = time.toString("_yyyy-MM-dd_hh-mm-ss");

		log_filename = portname + st +".log";

		qDebug()<<"start_test threadid:"<<QThread::currentThreadId()<<endl;
		com = new Com(portname,baudrate);
		td = new QThread();
		td->start();
		com->moveToThread(td);
		if(com->open()){
			QObject::connect(com,&Com::style_change,this,&console::set_consolecolor);
			QObject::connect(com,&Com::recv_data,this,&console::printToText);
		}
		script->create_console_object(portname,com);

	}
}

void console::on_textEdit_2_textChanged()
{
	QString text = ui->textEdit_2->toPlainText();
	int end = text.indexOf("\n");
	if(end != -1){
		QString line = text.mid(0,end);
		ui->textEdit_2->clear();
		QByteArray ba = line.toUtf8();
		ba.append("\r\n");
		//send_cmd(ba);
	}
}

